/**
 * 
 */
package org.openwork.portlet.skills.action;

import javax.servlet.http.HttpServletRequest;

import org.openwork.portlet.action.BaseAction;
import org.openwork.portlet.skillmanagement.service.SkillCategoryServiceUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;

/**
 * @author samuelgmartinez
 *
 */
public class UpdateSkillCategoryAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9107983367128157646L;

	public static final String UPDATE_SUCCESS_KEY = UpdateSkillCategoryAction.class.getName() + ".UpdateSuccess";
	public static final String ADD_SUCCESS_KEY = UpdateSkillCategoryAction.class.getName() + ".AddSuccess";
	
	@Override
	public String execute() throws Exception {
		HttpServletRequest request = getServletRequest();
		String idCategory = ParamUtil.get(request, WebKeys.SKILL_CAT_ID_PARAM, StringPool.BLANK);
		String categoryName = ParamUtil.get(request, "name", StringPool.BLANK);
		
		try {
			if(Validator.isNotNull(idCategory) && Validator.isNumber(idCategory) && 
					!StringPool.BLANK.equals(idCategory)) {
				SkillCategoryServiceUtil.updateSkillCategory(GetterUtil.getLong(idCategory), 
						categoryName, PortalUtil.getUserId(request), PortalUtil.getScopeGroupId(request));
				SessionMessages.add(request, UPDATE_SUCCESS_KEY);
			}
			else {
				SkillCategoryServiceUtil.addSkillCategory(categoryName, PortalUtil.getUserId(request), PortalUtil.getScopeGroupId(request));
				SessionMessages.add(request, ADD_SUCCESS_KEY);
			}			
		} catch(PortalException ex) {
			SessionErrors.add(request, ex.getClass().getName());
			return ERROR;
		}
		
		return SUCCESS;
	}
}
